﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class UcQuanLySoVang : UserControl
    {
        public UcQuanLySoVang()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        private void loaddata()
        {
            gridControlSoVang.DataSource = cn.getDataTable("tblSoVang_DS");            
        }
        private void UcQuanLySoVang_Load(object sender, EventArgs e)
        {
            loaddata();
        }

        private void gridViewSoVang_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gridViewSoVang.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.DeepSkyBlue;
            }
        }
        private void loadChiTiet()
        {
            SqlCommand cmd = new SqlCommand("tblUngHo_DS", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("stt", SqlDbType.Int)).Value = gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "STT");
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridControlUngHo.DataSource = dt;
        }
        private void gridViewSoVang_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            lbnguoiungho.Text = gridViewSoVang.GetFocusedRowCellDisplayText("HoTen");
            lbNgayUngHo.Text = gridViewSoVang.GetFocusedRowCellDisplayText("NgayUngHo");            
            lbNguoiNhan.Text = gridViewSoVang.GetFocusedRowCellDisplayText("HoTenNV");
            lbSoTien.Text = gridViewSoVang.GetFocusedRowCellDisplayText("SoTien");
            lbDiaChi.Text = gridViewSoVang.GetFocusedRowCellDisplayText("DiaChi");
            bientinhSoVang.Mamtq = Convert.ToInt32(gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "MaMTQ"));
            bientinhSoVang.Ngayungho=Convert.ToDateTime(gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle,"NgayUngHo"));
            bientinhSoVang.Hotennv = gridViewSoVang.GetFocusedRowCellDisplayText("HoTenNV");
            bientinhSoVang.Sttsovang = Convert.ToInt32(gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "STT"));
            bientinhSoVang.Diachi = gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "DiaChi").ToString();
            bientinhSoVang.Hotenmatq = gridViewSoVang.GetFocusedRowCellDisplayText("HoTen");
            if (gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "SoTien") == null)
            {
                bientinhSoVang.Sotien = 0;
            }
            else
            {
                bientinhSoVang.Sotien = Convert.ToDouble(gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "SoTien"));
            }
            try
            {
                loadChiTiet();

            }
            catch
            {
                                
            }
        }

        private void btnThemUngHo_Click(object sender, EventArgs e)
        {
            frmThemChiTietUngHo _frm = new frmThemChiTietUngHo();
            _frm.ShowDialog();
            loaddata();
            loadChiTiet();
        }

        private void btnUngHoTien_Click(object sender, EventArgs e)
        {
            bientinhSoVang.Thaotac = false;
            frmUngHoTien _frmUngHoTien = new frmUngHoTien();
            _frmUngHoTien.ShowDialog();
            loaddata();
        }

        private void gridViewUngHo_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gridViewUngHo.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.DeepSkyBlue;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa thông tin ủng hộ trong sổ vàng không?\n Nếu bạn chọn Yes mọi dữ liệu liên quan sẽ bị xóa hết!!!","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)                
            {

                    SqlCommand cmd = new SqlCommand("tblSoVang_Xoa", cn.ketnoi());
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("ma", SqlDbType.Int)).Value = gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "STT");
                    if (cmd.ExecuteNonQuery() == 1)
                    {
                        loaddata();
                        MessageBox.Show("Xóa thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Quá trình xóa bị lỗi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

            }
        }

        private void btnSuaUngHo_Click(object sender, EventArgs e)
        {
            bientinhSoVang.Thaotac = true;
            int vitri = gridViewSoVang.FocusedRowHandle;
            bientinhSoVang.Manv = Convert.ToString(gridViewSoVang.GetRowCellValue(gridViewSoVang.FocusedRowHandle, "nv"));
            if (double.Parse(gridViewSoVang.GetFocusedRowCellDisplayText("SoTien")) == 0)
            {
                frmSuaUngHoVatChat frm = new frmSuaUngHoVatChat();
                frm.ShowDialog();
                loaddata();
                gridViewSoVang.FocusedRowHandle = vitri;
            }
            else
            {
                bientinhSoVang.Thaotac = true;
                frmUngHoTien _frmunghotien = new frmUngHoTien();
                _frmunghotien.ShowDialog();
                loaddata();
                gridViewSoVang.FocusedRowHandle = vitri;
            }
            
        }

        private void btnPhieuCamTa_Click(object sender, EventArgs e)
        {
            if (bientinhSoVang.Sotien == 0)
            {
                frmInPhieuCamTa_VatChat frminphieuchivatchat = new frmInPhieuCamTa_VatChat();
                frminphieuchivatchat.ShowDialog();
            }
            else
            {
                frmInPhieuCamTa frmincamta_tien = new frmInPhieuCamTa();
                frmincamta_tien.ShowDialog();
            }
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

    }
}

